package com.lxk.mall.pms.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxk.mall.pms.controller.app.vo.ProductCategoryListReqVO;
import com.lxk.mall.pms.domain.PmsProductCategory;
import com.lxk.mall.pms.domain.vo.category.PmsProductCategoryVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;

import java.util.Collection;
import java.util.List;

/**
 * 商品分类Mapper接口
 *
 * @author Lion Li
 * @date 2024-06-06
 */
public interface PmsProductCategoryMapper extends BaseMapperPlus<PmsProductCategory, PmsProductCategoryVo> {

    default List<PmsProductCategory> selectListByStatus(Integer status) {
        return selectList(new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getStatus, status));
    }


    default List<PmsProductCategory> selectListByIdAndStatus(Collection<Long> ids, Integer status) {
        return selectList(new LambdaQueryWrapper<PmsProductCategory>()
            .in(PmsProductCategory::getId, ids)
            .orderByDesc(PmsProductCategory::getSort)
            .eq(PmsProductCategory::getStatus, status));
    }

    default List<PmsProductCategory> selectList(ProductCategoryListReqVO listReqVO) {
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(listReqVO.getName())) {
            lqw.eq(PmsProductCategory::getName, listReqVO.getName());
        }
        if (listReqVO.getParentId() != null) {
            lqw.eq(PmsProductCategory::getParentId, listReqVO.getParentId());
        }
        if (CollUtil.isNotEmpty(listReqVO.getParentIds())) {
            lqw.in(PmsProductCategory::getId, listReqVO.getParentIds());
        }
        if (listReqVO.getStatus() != null) {
            lqw.eq(PmsProductCategory::getStatus, listReqVO.getStatus());
        }
        lqw.orderByDesc(PmsProductCategory::getId);
        return selectList(lqw);
    }

    default Long selectCountByParentId(Long parentId){
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        return selectCount(lqw.eq(PmsProductCategory::getParentId, parentId));
    }
}
